package org.tea.inventory.service.impl;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import org.springframework.stereotype.Service;
import org.tea.inventory.base.contants.UserConstants;
import org.tea.inventory.base.util.StringUtils;
import org.tea.inventory.dao.tea.ProductCategoryMapper;
import org.tea.inventory.domain.tea.ProductCategory;
import org.tea.inventory.service.IProductCategoryService;
import org.tea.inventory.utils.UserUtil;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ProductCategoryServiceImpl implements IProductCategoryService {

    @Resource
    private ProductCategoryMapper productCategoryMapper;
    /**
     * 查询商品类别列表
     *
     * @param productCategory 商品类别信息
     * @return 商品类别集合信息
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory) {
        List<ProductCategory> productCategories = productCategoryMapper.selectProductCategoryList(productCategory);
        for (ProductCategory category : productCategories) {
            category.setCreateDateStr(DateUtil.format(category.getCreateDate(), DatePattern.NORM_DATETIME_MINUTE_PATTERN));
        }
        return productCategories;
    }
    /**
     * 检验商品类型编号是否重复
     *
     * @param productCategory 商品类别信息
     * @return
     */
    @Override
    public boolean checkCategoryIdUnique(ProductCategory productCategory) {
        ProductCategory info =  productCategoryMapper.checkCategoryIdUnique(productCategory);
        if (StringUtils.isNotNull(info)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
    /**
     * 新增商品类型
     *
     * @param productCategory 商品类别信息
     * @return
     */
    @Override
    public int insertProductCategory(ProductCategory productCategory) {
        productCategory.setCreateDate(new Date());
        productCategory.setCreateUserId(UserUtil.getUser().getLoginId());
        return productCategoryMapper.insertSelective(productCategory);
    }
    /**
     * 查询商品类别的子商品类别
     *
     * @param  id 商品类别Id
     * @return
     */
    @Override
    public boolean hasChildById(Long id) {
        int result = productCategoryMapper.hasChildById(id);
        return result > 0;
    }
    /**
     * 删除商品类别信息
     *
     * @param  id 商品类别Id
     * @return
     */
    @Override
    public int deleteById(Long id) {
        ProductCategory productCategory = new ProductCategory();
        productCategory.setId(id);
        productCategory.setDelFlag(1);
        productCategory.setUpdateDate(new Date());
        productCategory.setUpdateUserId(UserUtil.getUser().getLoginId());
        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }
    /**
     * 更新商品类别信息
     *
     * @param  productCategory 商品类别信息
     * @return
     */
    @Override
    public int update(ProductCategory productCategory) {
        productCategory.setUpdateDate(new Date());
        productCategory.setUpdateUserId(UserUtil.getUser().getLoginId());
        return productCategoryMapper.updateByPrimaryKeySelective(productCategory);
    }
    /**
     * 查询商品类别信息
     *
     * @param  id 商品类别信息Id
     * @return
     */
    @Override
    public ProductCategory selectByPrimaryKey(Long id) {
        return productCategoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean checkCodeUpdate(ProductCategory productCategory) {
        ProductCategory oldProductCategory = productCategoryMapper.selectByPrimaryKey(productCategory.getId());
        if (!oldProductCategory.getCategoryCode().equals(productCategory.getCategoryCode())) {
            return checkCategoryIdUnique(productCategory);
        }
        return true;
    }
}
